package cn.soft863.common.util;

/**
 * 分页的工具类
 * 
 * 总共36条记录,每页10条,问:共几页?4页 当前页 起始条数 1 1 2 11 3 21 4 31 起始条数如何算出来的? (页数 - 1)*10 +
 * 1
 * 
 * ~生成读写器,做容错处理
 * @author 902-B-Book
 *
 */
public class PageInfoUtil
{
	/* 每页多少条 */
	private int pageSize = 10;
	/* 当前页 */
	private int currentPage;
	/* 总条数 */
	private int totalRecord;

	/* 上一页 */
	private int prePage;
	/* 下一页 */
	private int nextPage;
	/* 总页数 */
	private int totalPage;
	/* 当前记录数(每页的起始条数) */
	private int currRecord;

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getCurrentPage()
	{
		if(this.currentPage < 1)
		{
			this.currentPage = 1; 
		}
		if(this.getTotalPage() > 0 && this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage() ; 
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getPrePage()
	{
		this.prePage = this.currentPage - 1 ; 
		if(this.prePage < 1 )
		{
			this.prePage = 1 ; 
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.currentPage + 1 ;
		if(this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage() ; 
		}
		return nextPage;
	}

	public int getTotalPage()
	{
		/* 
		 * 总页数
		 * 
		 * 总条数 / 10 + 1 
		 * 
		 * 总条数如果是30条,每页10条,问多少页?
		 * */
		this.totalPage = this.totalRecord / this.pageSize ; 
		if(this.totalRecord % this.pageSize != 0 )
		{
			this.totalPage = this.totalRecord / this.pageSize + 1;
		}
		return totalPage;
	}

	public int getCurrRecord()
	{
		/* 当前页的起始条数如何算呢? 
		 * 不能加+1,
		 * Mysql的limit?,?是从0开始的.
		 * */
		this.currRecord = (this.getCurrentPage() - 1 ) * this.getPageSize() ; 
		return currRecord;
	}

}
